

import pygame
import random

ScreenSize = [480, 700]

class BackGroundSprite(pygame.sprite.Sprite):
    """背景图精灵类"""
    def __init__(self,is_pre=False, speed=1):
        super().__init__()
        # 加载图片
        self.image = pygame.image.load('./images/background.png')
        # 获取图像矩形大小
        self.rect = self.image.get_rect()
        # 速度
        self.speed = speed
        # 判断是否是预备图片
        if is_pre:
            self.rect.y = -self.rect.h

    def update(self):
        """更新"""
        self.rect.y += self.speed
        if self.rect.y>=ScreenSize[1]:
            self.rect.y = -self.rect.h


class PlaneSprite(pygame.sprite.Sprite):
    """我方飞机精灵类"""
    def __init__(self,speed=1):
        super().__init__()
        self.image = pygame.image.load('./images/me1.png')
        self.rect = self.image.get_rect()
        self.rect.centerx = 240
        self.rect.centery = 630
        self.speed=speed

    def  move(self, num):
        self.rect.x+=num
        if self.rect.x<0:
            self.rect.x = 10
        elif self.rect.x>ScreenSize[0]-self.rect.w:
            self.rect.x = ScreenSize[0]-self.rect.w-10


class PlaneGame():
    """飞机大战游戏"""
    def __init__(self):
        # 初始化
        pygame.init()
        # 设置屏幕尺寸
        self.screen = pygame.display.set_mode(ScreenSize)
        # 设置飞机大战标题
        pygame.display.set_caption("飞机大战")
        self.is_loop = True
        # 是时钟
        self.clock = pygame.time.Clock()
        # 创建精灵类
        self.__create_sprite()
        pygame.time.set_timer(pygame.USEREVENT, 2000)



    def start(self):
        """启动游戏"""
        while self.is_loop:
            self.__event_handler()
            self.screen.fill([0,0,0])
            self.__update_sprite()
            # 现在每s最多60帧
            self.clock.tick(60)
            pygame.display.update()
        pygame.quit()

    def __event_handler(self):
        """事件处理"""
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_LEFT:
                    self.hero.move(-10)
                elif e.key == pygame.K_RIGHT:
                    self.hero.move(+10)
            elif e.type == pygame.USEREVENT:
                es = EnemySprite()
                self.es_group.add(es)



    def __create_sprite(self):
        # 创建对象
        bg = BackGroundSprite()
        bg1 = BackGroundSprite(True)
        # 将精灵对象添加到精灵组当中
        self.bg_group = pygame.sprite.Group(bg, bg1)
        self.hero = PlaneSprite()
        self.hero_group = pygame.sprite.Group(self.hero)
        self.es_group =pygame.sprite.Group()


    def __update_sprite(self):
        """更新精灵"""
        # 精灵组更新
        self.bg_group.update()
        self.es_group.update()
        #精灵组渲染
        self.bg_group.draw(self.screen)
        self.hero_group.draw(self.screen)
        self.es_group.draw(self.screen)

class EnemySprite(pygame.sprite.Sprite):
    """敌方飞机精灵类"""
    def __init__(self,speed=1):
        super().__init__()
        self.image = pygame.image.load('./images/enemy1.png')
        self.rect = self.image.get_rect()
        self.rect.x =random.randint(0, 480-self.rect.w)
        self.rect.y = 0
        self.speed=speed

    def update(self):
        """更新"""
        self.rect.y += self.speed
        if self.rect.y > 700:
            self.kill()



if __name__ == '__main__':
    plane = PlaneGame()
    plane.start()